# Maintainer: Wolfgang Stöggl <c72578@yahoo.de>

_realname=sox
_base_ver=14.4.2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# https://sourceforge.net/p/sox/code/ci/07de8a77a862e6800b95a8d3a61c6b4e41362755/
pkgver=14.4.2.r3203.07de8a77
pkgrel=9
pkgdesc="SoX is the Swiss Army Knife of sound processing utilities (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://sourceforge.net/projects/sox/"
license=("GPL")
makedepends=(autoconf-archive git
             "${MINGW_PACKAGE_PREFIX}-autotools"
             ${MINGW_PACKAGE_PREFIX}-flac
             "${MINGW_PACKAGE_PREFIX}-cc"
             ${MINGW_PACKAGE_PREFIX}-gsm
             ${MINGW_PACKAGE_PREFIX}-id3lib
             ${MINGW_PACKAGE_PREFIX}-lame
             ${MINGW_PACKAGE_PREFIX}-libao
             ${MINGW_PACKAGE_PREFIX}-libid3tag
             ${MINGW_PACKAGE_PREFIX}-libmad
             ${MINGW_PACKAGE_PREFIX}-libpng
             ${MINGW_PACKAGE_PREFIX}-libsndfile
             ${MINGW_PACKAGE_PREFIX}-autotools
             ${MINGW_PACKAGE_PREFIX}-libvorbis
             ${MINGW_PACKAGE_PREFIX}-opencore-amr
             ${MINGW_PACKAGE_PREFIX}-opusfile
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             ${MINGW_PACKAGE_PREFIX}-twolame
             ${MINGW_PACKAGE_PREFIX}-vo-amrwbenc
             ${MINGW_PACKAGE_PREFIX}-wavpack)
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
             ${MINGW_PACKAGE_PREFIX}-flac
             ${MINGW_PACKAGE_PREFIX}-gsm
             ${MINGW_PACKAGE_PREFIX}-id3lib
             ${MINGW_PACKAGE_PREFIX}-lame
             ${MINGW_PACKAGE_PREFIX}-libao
             ${MINGW_PACKAGE_PREFIX}-libid3tag
             ${MINGW_PACKAGE_PREFIX}-libmad
             ${MINGW_PACKAGE_PREFIX}-libpng
             ${MINGW_PACKAGE_PREFIX}-libsndfile
             ${MINGW_PACKAGE_PREFIX}-libltdl
             ${MINGW_PACKAGE_PREFIX}-libvorbis
             ${MINGW_PACKAGE_PREFIX}-opencore-amr
             ${MINGW_PACKAGE_PREFIX}-opusfile
             ${MINGW_PACKAGE_PREFIX}-twolame
             ${MINGW_PACKAGE_PREFIX}-vo-amrwbenc
             ${MINGW_PACKAGE_PREFIX}-wavpack)
options=('staticlibs' 'strip')
source=("${_realname}"::"git+https://git.code.sf.net/p/sox/code#commit=07de8a77a862e6800b95a8d3a61c6b4e41362755"
        "0001-ucrt-no-rewind-pipe.patch")
sha256sums=('SKIP'
            '693e7dcd737ea6c5137f2827518ff08549841d480cde00c4f3f505b2657c71f0')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}/0001-ucrt-no-rewind-pipe.patch"

  autoreconf -i
}

build() {
  # Silence format warnings originating from usage of PRIu64 and PRIuPTR within lsx_warn, lsx_debug and lsx_fail
  export CFLAGS="$CFLAGS -Wno-format"

  export lt_cv_deplibs_check_method='pass_all'

  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DFLAC__NO_DLL" \
  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --with-distro='MSYS2 MINGW-packages'

  make

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-distro='MSYS2 MINGW-packages'

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install
}
